﻿using System.Configuration;
using System.Windows.Input;
using MVVMFramework.ViewModel;
using System.Diagnostics;
using SharpSvn;

namespace SvnMonitor.Views.Shell
{
    class ShellVM : ViewModelBase
    {
        private bool isRunning;

        public ShellVM()
        {

        }

        #region Properties

        public string Title
        {
            get
            {
                return "Svn Monitor";
            }
        }

        private string repository;
        public string Repository
        {
            get
            {
                return repository;
            }
            set
            {
                if (repository != value)
                {
                    repository = value;
                    OnPropertyChanged(() => Repository);
                }
            }
        }

        private bool projectsLoaded;
        public bool ProjectsLoaded
        {
            get
            {
                return projectsLoaded;
            }
            private set
            {
                if (projectsLoaded != value)
                {
                    projectsLoaded = value;
                    OnPropertyChanged(() => ProjectsLoaded);
                }
            }
        }

        private RepositoryMonitorConfigSection section;
        public RepositoryMonitorConfigSection Section
        {
            get
            {
                return section;
            }
            private set
            {
                if (section != value)
                {
                    section = value;
                    OnPropertyChanged(() => Section);
                }
            }
        }

        #endregion

        #region Commands

        private ICommand openLogsCommand;
        public ICommand OpenLogsCommand
        {
            get
            {
                return openLogsCommand = openLogsCommand ?? new RelayCommand(() => Process.Start(AppSettings.Instance.LogPath));
            }
        }

        private ICommand connectToRepositoryCommand;


        public ICommand ConnectToRepositoryCommand
        {
            get
            {
                return connectToRepositoryCommand = connectToRepositoryCommand ?? new RelayCommand(() =>
                {
                    using (SvnClient client = new SvnClient())
                    {
                        SvnInfoEventArgs info;
                        SvnUriTarget uri = new SvnUriTarget(Repository);
                        client.GetInfo(uri, out info);

                        ViewManager.ShowMessage(info.LastChangeRevision.ToString(), "SVN");
                    }
                });
            }
        }

        #endregion

        public bool StartMonitor()
        {
            if (isRunning)
            {
                ViewManager.ShowMessage("Svn Monitor is already running.\n\r\n\rIf you want to rerun it, please restart application.", "Svn Monitor");
                return false;
            }

            if (Section == null)
            {
                ViewManager.ShowMessage("Svn Monitor settings have not been loaded from configuration file.", "Svn Monitor");
                return false;
            }

            new RepositoryMonitor(Section.Project, Section.WorkingCopy).Start();
            isRunning = true;

            return true;
        }

        protected override void LoadLookups()
        {
            loadProjectsFromConfig();

            base.LoadLookups();
        }

        private void loadProjectsFromConfig()
        {
            Section = ConfigurationManager.GetSection("RepositoryMonitor") as RepositoryMonitorConfigSection;
            if (Section == null)
            {
                // create section with default values
                Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location);
                config.Sections.Add("RepositoryMonitor", new RepositoryMonitorConfigSection());
                config.Save();

                ViewManager.ShowMessage("There are no settings in the application configuration file.\n\rDefault configuration settings has been created.\n\r\n\r" +
                                        "Please specify correct settings for minitoring in the configuration file and restart application.", "Svn Monitor");

            }
            else
            {
                ProjectsLoaded = true;
            }
        }
    }
}
